package com.jiulong.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.jiulong.common.utils.StringUtil;
import com.jiulong.model.TProduct;
import com.jiulong.service.ProductService;

@Controller
public class ljpControl extends BaseController {

    @Autowired
    ProductService productService;

    @RequestMapping(value = "/kfyd", method = RequestMethod.GET)
    public String kfyd(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        try {
            HttpSession session = request.getSession();
            session.getAttribute("openid");

        } catch (Exception e) {

        }
        List list = productService.findProductByDefault(null);

        model.addAttribute("detailList", list);
        return "kfyd";
    }

    @RequestMapping(value = "/kfydFenye", method = RequestMethod.GET)
    public String kfydFenye(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {

        List list = productService.findProductByDefault(null);
/*        model.addAttribute("currentPage", 2);
        model.addAttribute("totalPage", 20);
        model.addAttribute("pageSize", 2);
        model.addAttribute("totalRows", 100);
        */
        model.addAttribute("detailList", list);
        return "kfyd2";
    }

    @RequestMapping(value = "/kfydLone", method = RequestMethod.GET)
    public String kfyLone(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        List list = null;
        try {
            String productName = new String(request.getParameter("productName").getBytes("iso8859-1"), "utf-8");
            list = productService.findProductLone(productName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("detailList", list);
        return "kfydLone";
    }

    @RequestMapping(value = "/kfyd", method = RequestMethod.POST)
    public String postkfyd(String city, String startDate, String endDate, HttpServletRequest request, HttpServletResponse response, Model model) throws IOException, ParseException {

        String title = request.getParameter("title");
        String city1 = null;
        String city2 = null;
        String city3 = null;
        String check = "北京|上海|天津|重庆";

        if (!StringUtil.isBlank(city)) {
            String c[] = city.split(" ");
            city1 = c[0];
            city2 = c[1];
            if (check.indexOf(city1) == -1) {
                city3 = c[2];

            }
            if (city1.length() > 2) {
                city1 = city1.substring(0, 2);
            }

        }
/*		Date sd=null;
        Date ed=null;
		if(!StringUtil.isBlank(startDate)&&!StringUtil.isBlank(endDate)){
			SimpleDateFormat sdf2  = new SimpleDateFormat("yyyy-MM-dd");
			sd=sdf2.parse(startDate);
			ed=sdf2.parse(endDate);
		}
		*/
        HashMap param = new HashMap();
        param.put("city1", city1);
        param.put("city2", city2);
        param.put("city3", city3);
        //param.put("startDate",sd);
        //	param.put("endDate", ed);
        param.put("title", title);
			
	/*	Object o=breakfirst!=null?param.put("breakfirst",breakfirst.equals("on")?1:0):param.put("breakfirst", 0);
		o=park!=null?param.put("park",park.equals("on")?1:0):param.put("park", 0);
		o=meettingRoom!=null?param.put("meettingRoom",meettingRoom.equals("on")?1:0):param.put("meettingRoom", 0);
		o=wifi!=null?param.put("wifi",wifi.equals("on")?1:0):param.put("wifi", 0);
		o=visitor!=null?param.put("visitor",visitor.equals("on")?1:0):param.put("visitor", 0);*/
        List list = productService.findProductBysele(param);
        TProduct p = null;
        //System.out.println(1);
        if ((list.toString() == "[]")) {
            return "kfyd";
        }
        p = (TProduct) list.get(0);
        model.addAttribute("onlyId", p.getId());
        model.addAttribute("detailList", list);
        return "kfyd";
    }


    //┏┓　　　┏┓
    //┏┛┻━━━┛┻┓
    //┃　　　　　　　┃ 　
    //┃　　　━　　　┃
    //┃　┳┛　┗┳　┃
    //┃　　　　　　　┃
    //┃　　　┻　　　┃
    //┃　　　　　　　┃
    //┗━┓　　　┏━┛
//	    ┃      ┃
    //┃　　　┃  神兽保佑　　　　　　　　
    //┃　　　┃  代码无BUG！
    //┃　　　┗━━━┓
    //┃　　　　　　　┣┓
    //┃　　　　　　　┏┛
    //┗┓┓┏━┳┓┏┛
    // ┃┫┫　┃┫┫
    // ┗┻┛　┗┻┛

    @RequestMapping(value = "/AccountAction", method = RequestMethod.GET)
    public void UserLocaltion(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        System.out.println(account);

    }

    public static void main(String[] args) {

        String a = "B1ABEBIBOBUBABEBIBOBU";
        int s = a.indexOf("B ", 0);
        System.out.println(s);

    }


}
